package uk.ac.bath.legacy;


import java.io.PrintStream;
import java.io.StringWriter;
import uk.ac.bath.environment.MachineIF;



public abstract class Cell implements  uk.ac.bath.environment.cell.CellIF {

    
 
    protected MachineIF machine;
    protected Point point;
    public  double fitness;
    protected String tag;
    protected int id_;
    protected int age;
    protected int generation;
    protected int mutationCount;

    protected Cell(MachineIF machine,
            Point point,
            double fitness,
            int id,
            int generation,
            int mutations,
            String tag) {

        this.tag = tag;
        this.machine = machine;
        this.fitness = fitness;
        if (point != null) {
            this.point = (Point) (point.clone());
        }
        id_ = id;
        this.age = 0;
        this.generation = generation;
        this.mutationCount = mutations;

    }

 
    public String getTag() {
        return tag;
    }

    public int getAge() {
        return age;
    }

    public MachineIF getMachine() {
        return machine;
    }

    public int getGeneration() {
        return generation;
    }

    public double getFitness() {
        return fitness;
    }

    public Point getPoint() {
        return point;
    }

    public int getId() {
        return id_;
    }

    public void save(PrintStream out) {
        out.println(id_);
        out.println(age);
        out.println(fitness);
        point.save(out);

        StringWriter w = new StringWriter();
        machine.toString(w);
        out.println(w.toString());
    //	out.println(in_.toString());
    }

    public void debugPrint(PrintStream out) {
        out.print("#" + id_ + " loc=");
        point.save(out);
        out.print(" fit=" + fitness + " | ");
    }

    final public void tick() {
        age+=1;
        tickImpl();
    }


    abstract protected void tickImpl();

    static int idCount;

    static public int nextId() {
        return idCount++;
    }
    
    public void kill() {
        fitness = -1;
    }
}




